<?php
namespace Home\Controller;
use Think\Controller;
use Home\Aip\Store;
use Home\Model\OrderModel;

class SpStoreController extends CommonController {
	protected $table = 'store';
	protected $st_grade = 'store_grade';//店铺等级表
	protected $tb_goods = 'product';//产品数据表
	protected $sp_notb = 'shop_number';//平台货号标记表
	protected $tb_xianshi = 'p_xianshi';//限时折扣活动表
	protected $tb_xs_goods = 'p_xianshi_goods';//限时折扣商品表
	protected $tb_xianshi_st = 'p_xianshi_store';//店铺参与的活动表	
	protected $gc_class = 'goods_class';//商品分类表
	protected $tb_pay = 'payment' ;//支付方式表
	protected $tb_order = 'order' ;//订单表
	protected $tb_order_g = 'order_goods' ;//订单产品表
	
	/*
	*店铺首页
	*商家中心
	*/
    public function index(){
		//出售中的商品数量
		 unset($str);
		 unset($data);
		 $str = "id>%d and goods_state=%d and st_id=%d and goods_verify=%d";
		 $data = array(0,1,$_SESSION['shop_store']['store_id'],1);
		 $gs_num = M($this->tb_goods)->where($str,$data)->count();
		 //仓库中的商品数量
		 unset($str);
		 unset($data);
		 $str = "id>%d and goods_state=%d and st_id=%d and goods_verify=%d";
		 $data = array(0,0,$_SESSION['shop_store']['store_id'],1);
		 $dopts_num = M($this->tb_goods)->where($str,$data)->count();
		 //违规下架的商品数量
		 unset($str);
		 unset($data);
		 $str = "id>%d and goods_state=%d and st_id=%d and goods_verify=%d";
		 $data = array(0,10,$_SESSION['shop_store']['store_id'],0);
		 $gout_num = M($this->tb_goods)->where($str,$data)->count();
		 //待发货
		 unset($str);
		 unset($data);
		 $str = "order_id>%d and order_state=%d and store_id=%d";
		 $data = array(0,20,$_SESSION['shop_store']['store_id']);
		 $on_order = M($this->tb_order)->where($str,$data)->count();
		 //待付款
		 unset($str);
		 unset($data);
		 $str = "order_id>%d and order_state=%d and store_id=%d";
		 $data = array(0,10,$_SESSION['shop_store']['store_id']);
		 $on_pay = M($this->tb_order)->where($str,$data)->count();	
		 //交易中的订单
		 unset($str);
		 unset($data);
		 $str = "order_id>%d and order_state<%d and store_id=%d";
		 $data = array(0,40,$_SESSION['shop_store']['store_id']);
		 $join_order = M($this->tb_order)->where($str,$data)->count();			 
		// dump($gout_num);die;
		 $this->assign('gs_num',$gs_num) ;
		 $this->assign('dopts_num',$dopts_num) ;
		 $this->assign('gout_num',$gout_num) ;
		 $this->assign('on_order',$on_order) ;
		 $this->assign('on_pay',$on_pay) ;
		 $this->assign('join_order',$join_order) ;
		 $this->display();
    }
	
	/*
	*发布商品第一步
	*
	*/
    public function goods_add(){
			
		$this->assign('gid',I('get.goodsid')) ;
		if (I('get.class_id') && I('get.op')=='add_step_two') {
			//dump(I('post.'));die;
			//第二步
			$this->step_two();
		} elseif (I('get.commonid') && I('get.op')=='add_step_three') {
			//第三步
			 $this->step_three();
		} elseif (I('get.commonid') && I('get.op')=='add_step_four') {
			//第四步
			 $this->step_four();
		} else if (I('get.goodsid') && I('get.op')=='add_step_two') {
			//编辑第二步
			$this->step_two();
		} else {
			 $this->display();
		}
		
    }
	/*
	*发布商品第二步
	*
	*/
    public function step_two(){
		
		$stc = new Store();	
		
		if (I('get.op')=='add_step_two') {
			
			//商品编辑	
			unset($str);
			unset($data);
			session('goods_wts',null);
			$str = "id=%d ";
			$data = array(I('get.goodsid')) ;
			$goods_ff = _getOne($this->tb_goods,$str,$data) ;
			$goods_ff['shop_area_ids'] = unserialize($goods_ff['shop_area_ids']);
			$this->assign('shop_area',$goods_ff['shop_area_ids'][0]);
			//dump($goods_ff['shop_area_ids'][0]);die;
			if (I('get.class_id')) {
				$gc_id = I('get.class_id') ;  
			} else {
				$goods_ff['gc_id_2'] && $gc_id = $goods_ff['gc_id_2'] ;
				!$gc_id && $gc_id = $goods_ff['gc_id_1'] ;
				!$gc_id && $gc_id = $goods_ff['gc_id'] ;
			}
			
			//颜色
			$goods_color = strAnalysis($goods_ff['goods_color']) ;
			
			if (!$goods_color) {
				$goods_color = unserialize($goods_ff['goods_color']) ;
			}
			
			$goods_ff['goods_color'] = $goods_color ;
			
			$goods_color && $goods_ff['goods_c'] = 1 ;
			//规格
		    $goods_spec = strAnalysis($goods_ff['goods_spec']) ;
			
			if (!$goods_spec) {
				$goods_spec = unserialize($goods_ff['goods_spec']) ;
			}
			
			$goods_ff['goods_spec'] = $goods_spec ;
			
			$goods_spec && $goods_ff['goods_s'] = 2 ;
			$goods_ff['goods_spec_arr'] && $goods_ff['goods_spec_arr'] = unserialize($goods_ff['goods_spec_arr']) ;
			
			session('goods_wts',$goods_ff);

			//查询商品分类			
			$class = $stc ->ChackClass($gc_id) ;
			//dump(I('get.class_id'));die;
			if ($class['gc_name_3']) {
				$class = $class['gc_name_1']."&nbsp;>&nbsp;".$class['gc_name_2']." &nbsp;>&nbsp;".$class['gc_name_3'] ;
			} else if (!$class['gc_name_3'] && $class['gc_name_2']) {
				$class = $class['gc_name_1']."&nbsp;>&nbsp;".$class['gc_name_2'];
			} else {
				$class = $class['gc_name_1'] ;
			}
			//dump($goods_ff);die;
			$this->assign('goods_wts',$goods_ff) ;
			$this->assign('cate_id',$gc_id) ;
			$this->assign('class',$class) ;
			if ($class) {
					
				if (I('post.sizeinfo')) {	 
					if ($goods_ff['goods_spec_arr']) {
						$this->ajaxReturn($goods_ff['goods_spec_arr']);
					}							
				}
				
				$this->display('step_two');
			} else {
				$this->display();
			}
		} else {
			
			$this->display(); 
		}
		
    }
	/*
	*发布商品第三步
	*
	*/
    public function step_three(){
		
		
		if (IS_POST && I('get.commonid') && I('get.op')=='add_step_three') {
				 
			  //$datas ["cate_id"] = I('post.cate_id') ; //商品分类ID
			  //查找分类ID的父级ID
			  $getgrade_1 = _getOne($this->gc_class,'gc_id=%d',array(I('post.cate_id'))) ;
			  $getgrade_1 && $gc_1 = $getgrade_1 ['gc_parent_id'] ;
			  $gc_1 && $getgrade_2 = _getOne($this->gc_class,'gc_id=%d',array($gc_1)) ;
			  $getgrade_2 && $gc_2 = $getgrade_2 ['gc_parent_id'] ;
			  
			  if ($gc_2) {
				  $datas ["gc_1"] = $gc_2 ; //第1级分类
				  $datas ["gc_2"] = $gc_1 ; //第2级分类
				  $datas ["gc_3"] = I('post.cate_id') ; //第3级分类
			  } else if(!$gc_2 && $gc_1) {
				  $datas ["gc_1"] = $gc_1 ; //第1级分类
				  $datas ["gc_2"] = I('post.cate_id') ; //第2级分类
				  $datas ["gc_3"] = 0 ; //第3级分类				  
			  } else if (!$gc_1) {
				  $datas ["gc_1"] = I('post.cate_id') ; //第1级分类
				  $datas ["gc_2"] = 0 ; //第2级分类
				  $datas ["gc_3"] = 0 ; //第3级分类				  
			  }
			
			  I('post.cate_name')?$datas ["cate_name"] = I('post.cate_name'): die("<script>alert(商品分类不能为空);history.go(-1);</script>"); //商品分类名称
			  I('post.g_name')?$datas ["g_name"] = I('post.g_name') : die("<script>alert(商品名称不能为空);history.go(-1);</script>"); //商品名称
			  $datas ["g_jingle"] = I('post.g_jingle') ; //商品卖点
			  $datas ["g_price"] = I('post.g_price') ; //商品价格
			  $datas ["g_marketprice"] = I('post.g_marketprice') ; //市场价
			  $datas ["g_costprice"] = I('post.g_costprice') ; //成本价
			  $datas ["g_discount"] = I('post.g_discount') ; //折扣
			  I('post.g_storage')?$datas ["g_storage"] = I('post.g_storage'):die("<script>alert(库存不能为空);history.go(-1);</script>"); //库存
			  $datas ["g_alarm"] = I('post.g_alarm') ; //库存预警
			  $datas ["g_serial"] = I('post.g_serial') ; //商家货号
			  $datas ["image_path"] = I('post.image_path') ; //商品图片			  
			  $datas ["jumpMenu"] = I('post.jumpMenu') ; //手机导航图片（不需要）
			  $datas ["b_name"] = I('post.b_name') ; //品牌名称
			  $datas ["b_id"] = I('post.b_id') ; //品牌ID
			  $datas ["g_body"] = I('post.g_body') ; //商品详情pc端
			  $datas ["m_body"] = I('post.m_body') ; //商品详情手机端
			  
			  $datas ["g_deliverdate"] = I('post.g_deliverdate') ; //发货日期
			  $datas ["province_id"] = I('post.province_id') ; //省份ID
			  $datas ["city_id"] = I('post.city_id') ; //市区ID
			  $datas ["freight"] = I('post.freight') ; //运费类型 0为固定运费 1使用售卖区域
			  $datas ["g_freight"] = I('post.g_freight') ; //运费
			  $datas ["transport_id"] = I('post.transport_id') ; //售卖区域运费模板ID
			  $datas ["transport_title"] = I('post.transport_title') ; //售卖区域模板名称
			  $datas ["g_vat"] = I('post.g_vat') ; //是否开增值税发票 0否 1是
			  $datas ["sgcate_id"] = I('post.sgcate_id') ; //其他信息
			  $datas ["g_state"] = I('post.g_state') ; //商品发布状态 1立即发布 0放入仓库
			  $datas ["g_commend"] = I('post.g_commend') ; //商品推荐 1推荐 0不推荐
			  $spec =  I('post.sp_name');//商品规格
			 unset($color);
			 unset($format);

			 foreach ($spec[1] as $kk=>$vv) {
				 $spec[1][$kk] = $vv ;
				 if($vv['name'] && $vv['id'] && $vv['size']){					 
					 $spec[1][$kk]['size'] = substr($vv['size'],0,-1);  
				 }else{
					 unset($vv);
				 }			
			 }			  
   
			 foreach ($spec[2] as $key=>$v) {
				 $spec[2][$key] = $v ;
				 if($v['name'] && $v['id'] && $v['size']){				 
					 $spec[2][$key]['size'] = substr($v['size'],0,-1);  	
				 } else {
					 unset($v);
				 }
			 }
			 
			 is_array($spec[1]) ? $datas ["color"] = serialize($spec[1]) : $datas ["color"] = 0; //颜色
			 is_array($spec[2]) ? $datas ["spec"] = serialize($spec[2]) : $datas ["spec"] = 0; //规格

			  //新商品规格
			  if (I('post.spec')) {
				  $datas['goods_spec_arr'] = serialize(I('post.spec'));//商品规格			  
			  }
			  //dump($datas);die;
			  $datas['shop_area_ids'] = serialize(I('post.shop_area_ids'));//发布区域
			  $datas ["point_num"] = I('post.point_num') ; //商品积分价
			  $datas ["goods_state"] = I('post.goods_state') ; //商品状态
			  $datas ['give_coin'] = I('post.give_coin') ; //赠送增值币
			  $datas ['coin_num'] = I('post.coin_num') ; //增值币价格
			  
			  if ($datas) {
				  session('st_goods_add',null);
				  
				  $_SESSION['st_goods_add']["gc_id"] = $datas ["gc_1"] ;
				  $_SESSION['st_goods_add']["gc_id_1"] = $datas ["gc_2"] ;
				  $_SESSION['st_goods_add']["gc_id_2"] = $datas ["gc_3"] ;
				  $_SESSION['st_goods_add']["gc_name"] = $datas ["cate_name"] ;//不需要
				  $_SESSION['st_goods_add']["productname"] = $datas ["g_name"] ;
				  $_SESSION['st_goods_add']["goods_jingle"] = $datas ["g_jingle"] ;
				  $_SESSION['st_goods_add']["price"] = $datas ["g_price"] ;
				  $_SESSION['st_goods_add']["goods_marketprice"] = $datas ["g_marketprice"] ;
				  $_SESSION['st_goods_add']["goods_costprice"] = $datas ["g_costprice"] ;
				  $_SESSION['st_goods_add']["goods_discount"] = $datas ["g_discount"] ;
				  $_SESSION['st_goods_add']["goods_storage"] = $datas ["g_storage"] ;
				  $_SESSION['st_goods_add']["goods_alarm"] = $datas ["g_alarm"] ;
				 
				  $_SESSION['st_goods_add']["goods_number"] = $datas ["g_serial"] ;

				  $_SESSION['st_goods_add']["pic"] = $datas ["image_path"] ;
				  $_SESSION['st_goods_add']["jumpMenu"] = $datas ["jumpMenu"] ;//不需要
				  $_SESSION['st_goods_add']["bran_name"] = $datas ["b_name"] ;//不需要
				  $_SESSION['st_goods_add']["bran_id"] = $datas ["b_id"] ;
				  $_SESSION['st_goods_add']["content"] = htmlspecialchars($datas ["g_body"]);//商品详情pc端
				  
				  $_SESSION['st_goods_add']["moblie_body"] = $datas ["m_body"] ;//不需要
				  $_SESSION['st_goods_add']["goods_deliverdate"] = $datas ["g_deliverdate"] ;//不需要
				  $_SESSION['st_goods_add']["province_id"] = $datas ["province_id"] ;
				  $_SESSION['st_goods_add']["city_id"] = $datas ["city_id"] ;
				  $_SESSION['st_goods_add']["freight"] = $datas ["freight"] ;
				  $_SESSION['st_goods_add']["goods_freight"] = $datas ["g_freight"] ;
				  $_SESSION['st_goods_add']["transport_id"] = $datas ["transport_id"] ;
				  $_SESSION['st_goods_add']["transport_title"] = $datas ["transport_title"] ;//不需要
				  $_SESSION['st_goods_add']["goods_vat"] = $datas ["g_vat"] ;
				  $_SESSION['st_goods_add']["sgcate_id"] = $datas ["sgcate_id"] ;//不需要

				  $_SESSION['st_goods_add']["goods_state"] = $datas ["g_state"] ;//商品状态
				  $_SESSION['st_goods_add']["goods_commend"] = $datas ["g_commend"] ;

				  $_SESSION['st_goods_add']["goods_color"] = $datas ["color"] ; //颜色
 				  $_SESSION['st_goods_add']["goods_spec"] = $datas ["spec"] ; //规格
 
				  $_SESSION['st_goods_add']['shop_area_ids'] = $datas ['shop_area_ids'] ;	
				  $_SESSION['st_goods_add']['point_num'] = $datas ['point_num'] ;		
				  $_SESSION['st_goods_add']['give_coin'] = $datas ['give_coin'] ;
				
				 $_SESSION['st_goods_add']['coin_num'] = $datas ['coin_num'] ;				  
				 $_SESSION['st_g_state'] = $datas ["goods_state"] ;
				 $_SESSION['st_goods_add']['goods_spec_arr'] = $datas ['goods_spec_arr'] ;
				// dump($_SESSION['st_goods_add']);die;		
					
				  unset($datas);
			  }

			 //查询商品分类
			 $stc = new Store();				
			 $commonid = $stc ->ChackClass(I('get.commonid')) ;
			 if ($commonid) {
				
				 $this->assign('goods_wts',$_SESSION['goods_wts']);
				 $this->assign('commonid',I('get.commonid'));
				 $this->display('step_three');
			 } else {
				 session('st_goods_add',null);
				 $this->display();
			 }			  
			  
		} else {
			$this->display('step_two');
		}
    }
	
	/*
	*发布商品第四步
	*发布成功
	*/
    public function step_four(){
		
		if ($_SESSION['st_goods_add'] && IS_POST && I('post.form_submit')=='ok') {
			session('nctype',null);
			//图片集
			foreach (I('post.img') as $key=>$vv) {
				foreach ($vv as $k=>$v) {
					if ($v['name']) {
						$imgSet[] = $v ;
					}					
				}				
			}
			//dump(I('post.st_type'));die;
			$_SESSION['st_goods_add']['imgset'] = strColation($imgSet);
			//商品编辑、修改
			if (I('post.st_type')) {
			    $_SESSION['st_edit'] = I('post.st_type');
				$_SESSION['goodsid'] = I('post.goodsid');				
			}
			$this->goods_data_add();
		}

		if ($_SESSION['ok_timeOver']) {
			//商品发布成功超时强制退出
			goods_timeOver();	
			$goods_id = $_SESSION['st_goods']['goods_id'] ;
			$this->assign('goods_id',$goods_id) ;
			
			$this->display('step_four');
		} else {
			$this->display('/store/index');
		}
		
	}
		
	/*
	*
	*商品添加
	*编辑商品
	*/
    private function goods_data_add(){
		
		if (IS_POST && $_SESSION['st_goods_add']) {		

			//店铺ID
			unset($str);
			unset($data);
			$str = "member_id=%d and member_name='%s'";
			$data = array($_SESSION['user_id'],$_SESSION['user_name']) ;				
			$store_ff = _getOne($this->table,$str,$data) ; 	
			if ($store_ff['store_id'] && $store_ff['store_state']=='1') {
				$_SESSION['st_goods_add']['st_id'] = $store_ff['store_id'] ;
			} else {
				$this->error('店铺不存在','/shop/index');
			}
			
			 $_SESSION['st_goods_add']['minpic'] = thumb_img($_SESSION['st_goods_add']["pic"], $name = 'goods', $type=2 , $_SESSION['st_goods_add'], 300,300) ;//产品缩列图
			
			//添加时间 createtime
			!$_SESSION['st_edit'] && $_SESSION['st_goods_add']['createtime'] = time() ;
			//审核状态 goods_verify 商品审核 1通过，0未通过，10审核中
			!$_SESSION['st_edit'] && $_SESSION['st_goods_add']['goods_verify'] = 10 ;
			
			if ($_SESSION['st_g_state']==='10') {
				
				$_SESSION['st_goods_add']['goods_verify'] = 10 ;
			}
			//是否为直营店 is_own_shop
			!$_SESSION['st_edit'] && $_SESSION['st_goods_add']['is_own_shop'] = 0 ;
			
			if (!$_SESSION['st_edit']) {			
				//平台货号
				unset($str);
				unset($data);
				$str = "id>%d or number>%d";
				$data = array(0,999) ;
				$stnumber = _getOne($this->sp_notb,$str,$data) ; 
				$_SESSION['st_goods_add']["number"] = $stnumber ["number"]+ 1; 
			}
			foreach ($_SESSION['st_goods_add'] as $key=>$v) {
				if (!$v) {
					$_SESSION['st_goods_add'] [$key] = null;
				}
			}
			//$msg = array('','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','');
			//dump($_SESSION['st_goods_add']);die;		
			$join = new Store();
			$table = $this->tb_goods ;			
			
			if ($_SESSION['st_edit']=='edit') {
				//商品修改提交数据
				!$_SESSION['st_goods_add']["province_id"] && $_SESSION['st_goods_add']["province_id"] = $_SESSION['goods_wts']['province_id'] ;
				
				if (!$_SESSION['st_goods_add']["city_id"]) {
					$_SESSION['st_goods_add']["city_id"] = $_SESSION['goods_wts']["city_id"] ;
				} 
				
				unset($str);
				unset($data);
				$str = "id=%d";
				$data = array($_SESSION['goodsid']) ;					
				$ADD = $join -> _dataEdit($table,$str,$data,$_SESSION['st_goods_add'],'','st_goods_add');
				//删除缓存
				S('goods'.$_SESSION['goodsid'],null) ;
				session('st_edit',null);				
				$name = 'goodsid' ;
				$error = '修改成功！' ;
				$commdid = $_SESSION['goodsid'];
				$goodsid = $commdid ;
				session('goods_wts',null);
				session('goodsid',null);
			} else {
				unset($str);
				unset($data);
				$str = "id>%d or number=%d";
				$data = array(0,$stnumber ["number"]) ;
				$data2 ["number"] = $_SESSION['st_goods_add']["number"] ;				
				$no_deit =  _getEdit($this->sp_notb,$str,$data,$data2);
				
				//商品添加提交数据
				$ADD = $join -> _dataAdd($table,$_SESSION['st_goods_add'],'','st_goods_add');	
				$name = 'class_id' ;
				$error = '添加成功！' ;
				$commdid = $_SESSION['st_goods_add']["cate_id"] ;
				$goodsid = $ADD ;				
			}

			if (in_array($ADD, $msg)) {
				//清除所有S缓存
				SClear() ;
				$err = $ADD.'不能为空';
				$this->error($err,'/store/store_goods_add?op=add_step_two&'.$name.'='.$commdid);
			} else {
				$_SESSION['st_goods']['goods_id'] = $goodsid ;
				session('ok_timeOver',time());
				$this->error($error,'/store/add_step_four');
			}
		}
    }
	
	/*
	*获取分类名称
	*
	*/
    public function st_gc_grade(){
		if (IS_POST) {
			
			if (I('post.type')=='jax_gc') {
				//查询商品分类
				$stc = new Store();				
				$res = $stc ->ChackClass(I('post.id')) ;
				if (!$res) {
					$this->ajaxReturn(array('error'=>0));
				} 
				
				$list = _getList($this->gc_class,'gc_parent_id=%d',array(I('post.id')));
				if ($list) {
					$this->ajaxReturn(array('info'=>$list));
				} else {
					$this->ajaxReturn(array('error'=>0));
				}
			}
			
		}
    }
	
	/*
	*图片遍历
	*
	*/
    public function readyimg(){
		if (I('get.nctype')=='img_del' || I('get.nctype')=='img') {			
			//修改图片
			if (I('get.nctype')=='img' && !$_SESSION['nctype']) {
				 //图片集
				 
				$_SESSION['goods_wts']['imgset'] && $imgset = strAnalysis($_SESSION['goods_wts']['imgset']) ;
								 
				if ($_SESSION['goods_wts']['imgset']) {
					$this->ajaxReturn(array('info'=>2,'img'=>$imgset));
				} else {
					$this->ajaxReturn(array('info'=>1,'img'=>$_SESSION['st_goods_add']["pic"]));
				}
				
			} elseif (I('get.nctype')=='img_del') {
				$_SESSION['nctype'] = 'img_del';
				$this->ajaxReturn(array('info'=>3,'img'=>''));
			} else {
				$this->ajaxReturn(array('info'=>0));
			}
			
		}		
    }	
	/*
	*上传图片
	*
	*/
    public function pic_upload(){
		if (IS_POST) {
			//判断是否是店铺上传图片$_REQUEST['store']C:\Windows\system32\sscsp_cib.dll			
			$path = upload($_REQUEST['store']);	
			if ($path) {
				//清除图片集缓存
				$cache = 'img_dir'.$_SESSION['user_id'];
				S($cache,null);
			}
			$this->ajaxReturn(array('info'=>$path));
		}
    }
	
	/*
	*店铺图片集
	*
	*/
    public function image_set(){
		if (IS_POST) {
			if ($_SESSION['user_id']) {
				unset($str);
				unset($data);
				$cache = 'img_dir'.$_SESSION['user_id'];
				$str = "member_id=%d and member_name='%s'";
				$data = array($_SESSION['user_id'],$_SESSION['user_name']) ;		
				$store = _getOne($this->table,$str,$data) ; 
				if ($store['store_state']=='1') {
					$dir = dirname(dirname(dirname(dirname(__FILE__)))) ;
					$dir = $dir.'/Public/upload/store/'.$_SESSION['user_id'] ;
					$stcs = new Store();
					$file = $stcs ->read_all_dir($dir,$cache);
					//店铺图片集
					foreach ($file as $key=>$vv) {
						foreach ($vv as $k=>$v) {
							$arr_img [] = $v ;						
						}				
					}
					//dump($arr_img);die;
					$this->ajaxReturn($arr_img);					
				}				
			}
			if (!$arr_img) {
				$this->ajaxReturn(0);	
			}	
		}
    }
	
	/*
	*销售中的商品
	*
	*/
    public function st_goods_list(){

			if (!$_GET['p']) {
				$_GET['p'] = 1;
			}
			
			$num = 15;
			$order = 'id desc';
			$cache = 'goods_list'.$_GET['p'].'_'.$_SESSION['user_id'];
			unset($str);
			unset($data);
			session('goods_wts',null);
			$str = "id>%d and goods_state=%d and st_id=%d and goods_verify=%d";
			$data = array(0,1,$_SESSION['shop_store']['store_id'],1);	
			//分页
			$show = Page($this->tb_goods,$str,$data,$num);		
			//强制刷新
			_Refresh(1,$cache);	
			//关联查询
			$list=_getList($this->tb_goods,$str,$data,$cache,$order,$_GET['p'],$num);		
			//dump($list);die;
			
			$this->assign('page',$show);// 赋值分页输出
			$this->assign('list',$list);		
			$this->display();
    }
	
	/*
	*仓库中的商品
	*
	*/
    public function st_depot_list(){

			if (!$_GET['p']) {
				$_GET['p'] = 1;
			}
			
			$num = 20;
			$order = 'id desc';
			$cache = 'depot_list'.$_GET['p'].'_'.$_SESSION['user_id'];
			unset($str);
			unset($data);
			session('goods_wts',null);
			$str = "id>%d and goods_state=%d and st_id=%d and goods_verify=%d";
			$data = array(0,0,$_SESSION['shop_store']['store_id'],1);	
			//分页
			$show = Page($this->tb_goods,$str,$data,$num);		
			//强制刷新
			_Refresh(1,$cache);	
			//关联查询
			$list=_getList($this->tb_goods,$str,$data,$cache,$order,$_GET['p'],$num);		
			//dump($_SESSION['shop_store']['store_id']);die;
			$this->assign('page',$show);// 赋值分页输出
			$this->assign('list',$list);		
			$this->display();
    }
	/*
	*删除商品
	*
	*/
    public function st_goods_del(){
		if (IS_POST) {
			$massege = '删除成功！' ;
			$error = '删除失败！' ;	
			
			if (!$_GET['p']) {
				$_GET['p'] = 1;
			}
			if (I('post.state')=='1') {
				$cache = 'goods_list'.$_GET['p'].'_'.$_SESSION['user_id'] ;
			} else {
				$cache = 'depot_list'.$_GET['p'].'_'.$_SESSION['user_id'] ;
			}
			unset($data);
			unset($str);
			$str = "id=%d and goods_state=%d and st_id=%d";
			$data = array(I('post.id'),I('post.state'),$_SESSION['shop_store']['store_id']);				
			$del = _getDel($this->tb_goods,$str,$data,$cache);		
								
			if ($del) {							
				$this->ajaxReturn(array('info'=>$massege));			
			} else {
				$this->ajaxReturn(array('info'=>$error));		
			}					
		}
    }	

	/*
	*商品上架\下架
	*
	*/
    public function st_data_on(){
		if (IS_POST) {
			
			if (!$_GET['p']) {
				$_GET['p'] = 1;
			}
			
			$arr_id = explode(',',I('post.id')) ;
			$arr_state = explode(',',I('post.state')) ;
			
			if ($arr_state[0]=='0') {
				$cache = 'depot_list'.$_GET['p'].'_'.$_SESSION['user_id'] ;
				$state = 0 ;
				$get_state = 1 ;
				$massege = '下架成功！' ;
				$error = '下架失败！' ;					

			} else {
				
				$cache = 'goods_list'.$_GET['p'].'_'.$_SESSION['user_id'] ;
				$state = 1 ;
				$get_state = 0 ;
				$massege = '上架成功！' ;
				$error = '上架失败！' ;	
			}
			
			foreach ($arr_id as $key => $v) {
				if ($v) {
					unset($str);
					unset($data);
					unset($data2);
					if ($state) {
						$getOne = _getOne($this->tb_goods,'id=%d',array($v)) ;
						if ($getOne['goods_state']=='10'){
							$this->ajaxReturn(array('info'=>$error));	
						}
					}
					$data2 ['goods_state'] = $state ;
					$str = "id=%d and goods_state=%d and st_id=%d and goods_state<>%d";
					$data = array($v,$get_state,$_SESSION['shop_store']['store_id'],10);									
					$edit = _getEdit($this->tb_goods,$str,$data,$data2,$cache) ;
					unset($cache2);
					$cache2 = 'goods'.$v ;	
					S($cache2,null);
				}
			}
			
			if ($edit) {
							
				$this->ajaxReturn(array('info'=>$massege));			
			} else {
				$this->ajaxReturn(array('info'=>$error));		
			}					
					
		}
    }

	/*
	*违规下架
	*/
    public function st_gotout_list(){
		
			if (!$_GET['p']) {
				$_GET['p'] = 1;
			}
			
			$num = 20;
			$order = 'id desc';
			$cache = '';
			unset($str);
			unset($data);
			session('goods_wts',null);
			$str = "id>%d and goods_state=%d and st_id=%d and goods_verify=%d";
			$data = array(0,10,$_SESSION['shop_store']['store_id'],0);	
			//分页
			$show = Page($this->tb_goods,$str,$data,$num);		
			//强制刷新
			_Refresh(1,$cache);	
			//关联查询
			$list=_getList($this->tb_goods,$str,$data,$cache,$order,$_GET['p'],$num);		
			//dump($list);die;
			$this->assign('page',$show);// 赋值分页输出
			$this->assign('list',$list);		
			$this->display();
    }
	
	/*
	*待审核的商品
	*/
    public function st_goods_await_list(){
		
			if (!$_GET['p']) {
				$_GET['p'] = 1;
			}
			
			$num = 20;
			$order = 'id desc';
			$cache = '';
			unset($str);
			unset($data);
			session('goods_wts',null);
			$str = "id>%d and goods_state=%d and st_id=%d and (goods_verify=%d or goods_verify=%d)";
			$data = array(0,1,$_SESSION['shop_store']['store_id'],10,0);	
			//分页
			$show = Page($this->tb_goods,$str,$data,$num);		
			//强制刷新
			_Refresh(1,$cache);	
			//关联查询
			$list=_getList($this->tb_goods,$str,$data,$cache,$order,$_GET['p'],$num);		
			//dump($list);die;
			$this->assign('page',$show);// 赋值分页输出
			$this->assign('list',$list);		
			$this->display();
    }
	
	/**
	*店铺设置
	*
	*/	
	public function st_store_setting(){	
		$shop_store = $_SESSION['shop_store'] ;
		//主营商品
		$store_zy =  strAnalysis($shop_store ['store_zy']) ;
		$name = '';
		foreach ($store_zy as $zy => $vo) {			
			$vo['name'] && $name .= $vo['name'].',' ;
			$vo['son_1'] && $name .= $vo['son_1'].',' ;
			$vo['son_2'] && $name .= $vo['son_2'].',' ;			
		}
		$name = explode(',',$name) ;
		//去掉重复
		$name = array_unique($name) ;
		$name2 = '' ;
		foreach ($name as $key) {
			$key && $name2 .= $key.',' ;
		}
		//dump($name2);die;
		if ($name2) {
			$name2 = substr($name2,0,-1) ;
		}								
		$shop_store ['store_zy'] = $name2 ;
		//店铺等级	
		//dump($shop_store);die;
		$this->assign('shop_store',$shop_store) ;
		$this->display();		
	}
	
	/**
	*店铺设置提交
	*
	*/	
	public function submit_store_set(){	
	
		if (IS_OST && I('post.set_submit')=='ok') {
			//提交hash防止数据重复提交
			$hash = shopHash('st_set_hash',I('post.set_hash'));
			if ($hash) {
				unset($data);
				$data ['store_name'] = I('post.store_name') ;//店铺名称
				I('post.store_label') && $data ['store_label'] = I('post.store_label') ;//店铺logo
				I('post.store_banner') && $data ['store_banner'] = I('post.store_banner') ;//店铺条幅
				I('post.store_avatar') && $data ['store_avatar'] = I('post.store_avatar') ;//店铺头像
				I('post.store_qq') && $data ['store_qq'] = I('post.store_qq') ;//QQ
				I('post.store_ww') && $data ['store_ww'] = I('post.store_ww') ;//阿里旺旺
				I('post.store_phone') && $data ['store_phone'] = I('post.store_phone') ;//店铺电话
				I('post.seo_keywords') && $data ['store_keywords'] = htmlspecialchars(I('post.seo_keywords')) ;//SEO关键字
				I('post.seo_description') && $data ['store_description'] = htmlspecialchars(I('post.seo_description')) ;//SEO店铺描述

				if ($data) {
					//主营商品
					if (I('post.store_zy')) {
						$data ['store_zy'] = array();
						$store_zy = explode(',',I('post.store_zy')) ;
						
						foreach ($store_zy as $key=>$vv) {						
							$data ['store_zy'][$key]['name'] = $vv ;							
						}										
					}
					I('post.store_zy') && $data ['store_zy'] = strColation($data ['store_zy']) ;
						
					$msg = array('店铺名称');
						
					$join = new Store();
					$table = $this->table ;	
					//店铺设置修改提交数据
					unset($str);
					unset($data2);
					$str = "store_id=%d and member_id=%d and store_state=%d";
					$data2 = array(I('post.id'),$_SESSION['user_id'],1) ;
					$name = 'shop_store' ;	
					$edit = $join -> _dataEdit($table,$str,$data2,$data,$msg,$name);
					//dump($data);die;
					unset($data);
					if ($edit) {
						$this->error('修改成功！','/store/store_set');
					} else {
						$this->error('修改失败！','/store/store_set');
					}
				}
				
			} else {
				$this->error('请刷新页面！','/store/store_set');
			}
		}else {
			$this->error('修改失败！','/store/store_set');
		}				
	}
	
	/**
	*店铺信息
	*
	*/	
	public function st_store_datum(){
		$result = S('store_joinin_' . session('user_id'));
		if(!$result) {
			unset($result);
			$result = M('store_joinin') -> where('member_id = 8259') -> find();
			S('store_joinin_' . session('user_id'), $result);
		}
		$names = strAnalysis($result['store_class_names']);
		$commis_rates = strAnalysis($result['store_class_commis_rates']);
		$result['sc_class'] = array();

		for($i=0;$i<count($names);$i++) {
			$ns = explode(',',$names[$i]);
			$result['sc_class'][$i] = array('gc_name' => $ns[0], 'son_1' => $ns[1], 'bail' => $commis_rates[$i]);
		}
		$this->assign('joinin', $result);
		$this->display();		
	}
	
	/**
	*商城折扣活动
	*
	*/	
	public function st_store_xianshi(){	
		if (!$_GET['p']) {
			$_GET['p'] = 1;
		}
			
		$num = 5;
		$order = 'xianshi_id desc';
		unset($str);
		unset($data);
		$str = "xianshi_id>%d and state=%d and xianshi_type<>%d";
		$data = array(0,1,3);	
		//分页
		$show = Page($this->tb_xianshi,$str,$data,$num);		
		
		//查询
		$list = _getList($this->tb_xianshi,$str,$data,'',$order,$_GET['p'],$num) ;
		foreach ($list as $key=>$v) {
			if ($v['end_time']<=time()) {
				//_getEdit($this->tb_xianshi,$str,$data,array('state'=>2)) ;
				$list[$key] ['state_type'] = '活动已过期' ;
			}
		}
		//dump($list);die;
		$this->assign('page',$show);// 赋值分页输出
		$this->assign('list',$list);		
		$this->display();		
	}

	/**
	*店铺参与的活动
	*
	*/	
	public function st_store_xianshi_my(){	
		if (!$_GET['p']) {
			$_GET['p'] = 1;
		}
			
		$num = 15;
		$order = 'xs_id desc';
		unset($str);
		unset($data);
		$str = "st_id=%d and xs.state=%d ";
		$data = array($_SESSION['shop_store']['store_id'],1);		
		$join = ' LEFT JOIN __P_XIANSHI__ AS xs ON xs.xianshi_id=xs_id ' ;
		$field = 'xs.*' ;	
		//分页
		$show = Page($this->tb_xianshi_st,$str,$data,$num,$join);		
		
		//查询
		$list = _getJoinList($this->tb_xianshi_st,$join,$str,$data,'',$order,$peg,$num,$field);	
		
		//dump($str);die;
		$this->assign('page',$show);// 赋值分页输出
		$this->assign('list',$list);		
		$this->display();		
	}	


	/**
	*申请参与的活动
	*
	*/	
	public function aplay_xianshi(){	
		if (IS_POST) {
			if (I('post.type')=='st_aplay') {
				unset($data);
				$data ['xs_id'] = I('post.id') ;
				
				if (!I('post.id')) {
					 $data ['xs_id'] = $_SESSION ['my_xs_id'] ;
				}				
				!$data ['xs_id'] && $this->error('非法操作！','shop_xianshi') ;				
				session('my_xs_id',null);
				
				$str = 'xianshi_id=%d and state=%d and end_time>%d' ;
				$arr = array($data ['xs_id'],1,time()) ;
				$gcOne = _getOne($this->tb_xianshi,$str,$arr);
				if (!$gcOne) {
					$this->error('活动不存在或者已过期！','shop_xianshi') ;
				}
				
				unset($str);
				unset($arr);
				$str = 'xs_id=%d and st_id=%d' ;
				$arr = array($data ['xs_id'],$_SESSION['shop_store']['store_id']) ;
				$xsOne = _getOne($this->tb_xianshi_st,$str,$arr);
				if ($xsOne) {
					$this->error('您已经申请该活动！','shop_xianshi') ;
				}
				
				if ($gcOne['xianshi_shenhe']=='2') {
					
					$data ['p_state'] = 1 ;
				} else {
					$data ['p_state'] = 0 ;
				}
				
				$_SESSION['user_id'] ? $data ['member_id'] = $_SESSION['user_id'] :$this->error('请先登录！','/login') ;
				$_SESSION['user_name'] ? $data ['member_name'] = $_SESSION['user_name'] :$this->error('请先登录！','/login') ;
				$_SESSION['shop_store']['store_id'] ? $data ['st_id'] = $_SESSION['shop_store']['store_id'] :$this->error('请先登录！','/login') ;
				$_SESSION['shop_store']['store_name'] ? $data ['st_name'] = $_SESSION['shop_store']['store_name'] :$this->error('请先登录！','/login') ;
				
				//dump(I('post.'));die;
				
				$add = _getAdd($this->tb_xianshi_st,$data);
				
				if ($add) {
					if (I('post.xianshi_name')) {
						return 1 ;
					} else {
						$this->ajaxReturn(array('info'=>'申请成功，请等待审核！'));	
					}
					
				} else {
					if (I('post.xianshi_name')) {
						return 0 ;
					} else {
						$this->ajaxReturn(array('info'=>'申请失败！'));
					}
					
				}
			}
		}
	}	

	/**
	*促销活动商品管理
	*
	*/	
	public function st_goods_xianshi(){	

		unset($str);
		unset($data);
		$str = "xianshi_id=%d and state=%d ";
		$data = array(I('get.xianshi_id'),1);
		
		//查询
		$xianshi = _getOne($this->tb_xianshi,$str,$data) ;			
		if (!$xianshi) {
			$this->error('页面不存在！','shop_xianshi') ;
		}
		
		unset($str);
		unset($data);
		$str = "xs_id=%d and st_id=%d and p_state=%d";
		$data = array(I('get.xianshi_id'),$_SESSION['shop_store']['store_id'],1);
		
		//查询
		$xs_st = _getOne($this->tb_xianshi_st,$str,$data) ;
		if (!$xs_st) {
			$this->error('页面不存在！','shop_xianshi') ;
		}
		
		
		if (!$_GET['p']) {
			$_GET['p'] = 1;
		}			
		$num = 10;
		$order = 'xianshi_goods_id desc';
		
		unset($str);
		unset($data);
		$str = "xianshi_id=%d and store_id=%d";
		$data = array(I('get.xianshi_id'),$_SESSION['shop_store']['store_id']);	
		//分页
		$show = Page($this->tb_xs_goods,$str,$data,$num);		
		
		$xs_list = _getList($this->tb_xs_goods,$str,$data,'',$order,$_GET['p'],$num) ;	
		foreach ($xs_list as $key => $v) {
			$xianshi_discount = $v['xianshi_price'] / $v['goods_price'] * 10 ;
			$xs_list [$key] ['xianshi_discount'] = number_format($xianshi_discount,1) ;	
			unset($xianshi_discount);		
		}
		//dump($xs_list);die;
		$this->assign('page',$show);
		$this->assign('xs_list',$xs_list);		
		$this->assign('xianshi',$xianshi);		
		$this->display();		
	}
	
    /**
     * 选择活动商品
     **/
    public function goods_select(){
		$num = 10;
		unset($str);
		unset($data);
		unset($order);
		$order = 'id desc';
		$str = " goods_state=%d and goods_verify=%d and productname LIKE '%s'";
		$data = array(1,1,'%'.$_GET['goods_name'].'%');	
		//分页
		$show = Page($this->tb_goods,$str,$data,$num);		
		
		$goods_list = _getList($this->tb_goods,$str,$data,'',$order,$_GET['p'],$num) ;	
		foreach ($goods_list as $key => $v) {
			$goods_list [$key]['price'] = number_format($v['price']) ;
		}
		//dump($goods_list);die;
		$this->assign('page',$show);
		$this->assign('goods_list',$goods_list);
		$this->display('goods_select');		
    }		


    /**
     * 店铺添加活动
     **/
    public function st_xianshi_add() {
		if (IS_POST) {
			//添加
			unset($data);
			I('post.xianshi_name') ? $data ['xianshi_name'] = I('post.xianshi_name') :$this->error('活动名称不能为空','st_xianshi') ;
			I('post.xianshi_title') && $data ['xianshi_title'] = I('post.xianshi_title') ;
			I('post.xianshi_explain') && $data ['xianshi_explain'] = I('post.xianshi_explain')  ;
			I('post.start_time') ? $data ['start_time'] = strtotime(I('post.start_time')) :$this->error('开始时间不能为空','st_xianshi') ;
			
			I('post.end_time') ? $data ['end_time'] = strtotime(I('post.end_time')) :$this->error('结束时间不能为空','st_xianshi') ;
			
			if ($data ['end_time'] < $data ['start_time']) {
				$this->error('结束时间必须大于开始时间!','st_xianshi') ;
			}

			I('post.lower_limit') ? $data ['lower_limit'] = I('post.lower_limit') : $data ['lower_limit'] = 0 ;
			
			$data ['xianshi_type'] = 3; 			
			$data ['xianshi_shenhe'] = 2 ;			
			$data ['goods_number'] =  0 ;
			
			$_SESSION['user_id'] ? $data ['member_id'] = $_SESSION['user_id'] :$this->error('请先登录！','/login') ;
			$_SESSION['user_name'] ? $data ['member_name'] = $_SESSION['user_name'] :$this->error('请先登录！','/login') ;
			$_SESSION['shop_store']['store_id'] ? $data ['store_id'] = $_SESSION['shop_store']['store_id'] :$this->error('请先登录！','/login') ;
			$_SESSION['shop_store']['store_name'] ? $data ['store_name'] = $_SESSION['shop_store']['store_name'] :$this->error('请先登录！','/login') ;
			//$this->error('添加成功！','st_xianshi')	;
			//dump($data);die;
			if (I('post.xianshi_id')) {				
				unset($str) ;
				unset($arr) ;
				$str = "xianshi_id=%d";
				$arr = array(I('post.xianshi_id')) ;
				$gcOne = _getOne($this->tb_xianshi,$str,$arr);
				if ($gcOne['state']=='0' || $gcOne['xianshi_type']!=='3') {
					$this->error('非法操作！','st_xianshi')	;
				}
				$edit =  _getEdit($this->tb_xianshi,$str,$arr,$data);
				if ($edit) {
					$this->error('修改成功！','st_xianshi')	;
				} else {
					$this->error('修改失败！','st_xianshi')	;
				}
			} else {
				
				$add = _getAdd($this->tb_xianshi,$data);
				if ($add) {
					session('my_xs_id',$add);
					$adds = $this->aplay_xianshi() ;
					if ($adds) {
						$this->error('添加成功！','st_xianshi')	;
					} else {
						$this->error('添加失败！','st_xianshi')	;
					}
					
				} else {
					$this->error('添加失败！','st_xianshi')	;
				}				
			}

		} else if (I('get.xianshi_id') && I('get.type')=='edit') {
			//修改页面
			unset($str);
			unset($data);
			$str = "xianshi_id=%d and xianshi_type=%d";
			$data = array(I('get.xianshi_id'),3);		
			
			//查询
			$xianshi = _getOne($this->tb_xianshi,$str,$data) ;
			if ($xianshi) {
				//dump($xianshi);die;
				$this->assign('type','edit') ;				
				$this->assign('xianshi',$xianshi) ;
				$this->display('xianshi_add');					
			} else {
				$this->error('页面不存在','st_xianshi');
			}
		
			
		}  else if (I('get.type')=='add') {
			//新增页面
			$this->assign('type','add') ;				
			$this->display('xianshi_add');	
		} else {
			$this->display();	
		}
			
    }	

	 /**
     * 删除限时折扣
     **/
      public function  st_xianshi_del() {
		  if (IS_POST) {
			  
			  unset($str);
			  unset($data);
			  $type = I('post.type');
			  switch ($type) {
				case 'xianshi_del':
					$table = $this->tb_xianshi;
					unset($str7);
					unset($data7);
					$str7 = "p_id>%d and xs_id=%d";
					$data7 = array(0,I('post.id'));
					//删除促销活动下的所有店铺
					$list = _getList($this->tb_xianshi_st,$str7,$data7) ;
					if ($list) {
						foreach ($list as $key => $v) {		
							_getDel($this->tb_xianshi_st,array('p_id'=>$v['p_id']));					
						}					
					}
					unset($list);
					unset($str7);
					unset($data7);
					$str7 = "xianshi_goods_id>%d and xianshi_id=%d";
					$data7 = array(0,I('post.id'));
					//删除促销活动下的所有产品
					$list = _getList($this->tb_xs_goods,$str7,$data7) ;
					if ($list) {
						foreach ($list as $key => $v) {		
							_getDel($this->tb_xs_goods,array('xianshi_goods_id'=>$v['xianshi_goods_id']));					
						}					
					}
					$str = 'xianshi_id' ;
				break;				  
			  }

				$str = $str."=%d" ;
				$data = array(I('post.id'));			  
				if ($table) {
					$del = _getDel($table,$str,$data);	
				}	
				$massege = '删除成功！' ;
				$error = '删除失败！' ;						
				unset($str);
				unset($data);
				unset($gcOne);	
				unset($str2);
				unset($data2);
				unset($where);			
				if ($del) {	
					//清楚缓存
					SClear() ;				
					$this->ajaxReturn(array('info'=>$massege));				
				} else {
					$this->ajaxReturn(array('info'=>$error));	
				}
		  }
		  
	  }

    /**
     * 添加活动商品
     **/
      public function  st_xianshi_goods_add() {
		  if (IS_POST) {
			  
			  unset($str);
			  unset($data);
			  $type = I('post.type');
			  switch ($type) {
				case 'xianshi_goods_add':
					$table = $this->tb_xs_goods;
					unset($str);
					unset($data);
					$str = "id=%d and goods_state=%d and goods_verify=%d";
					$data = array(I('post.goods_id'),1,1) ;
					
					$goods_one = _getOne($this->tb_goods,$str,$data) ;
					if (!$goods_one) {
						$this->ajaxReturn(array('message'=>'商品不存在！'));
					}
					unset($str);
					unset($data);
					$str = "goods_id=%d and store_id=%d and xianshi_id=%d";
					$data = array(I('post.goods_id'),$_SESSION['shop_store']['store_id'],I('post.xianshi_id')) ;
					
					$xs_goods = _getOne($this->tb_xs_goods,$str,$data) ;
					if ($xs_goods) {
						$this->ajaxReturn(array('message'=>'商品已添加！'));
					}
					unset($str);
					unset($data);
					$str = "xianshi_id=%d and state=%d";
					$data = array(I('post.xianshi_id'),1) ;
					//查询
					$xianshi_one = _getOne($this->tb_xianshi,$str,$data) ;
					if (!$xianshi_one) {
						$this->ajaxReturn(array('message'=>'活动不存在！'));
					}
				
					if ($xianshi_one['goods_number']>0) {
						S('home_xianshi',null) ;
						unset($str);
						unset($data);
						//参加活动产品数量限制
						$str = "xianshi_id=%d and store_id=%d";
						$data = array(I('post.xianshi_id'),$_SESSION['shop_store']['store_id']) ;	
						$count = M($this->tb_xs_goods)->where($str,$data)->count();
						if ($count>=$xianshi_one['goods_number']) {
							$this->ajaxReturn(array('message'=>'添加商品数量超过活动允许的数量！'));
						}
					}
					unset($str);
					unset($data);
					$str = "xs_id=%d and p_state=%d and st_id=%d";
					$data = array(I('post.xianshi_id'),1,$_SESSION['shop_store']['store_id']) ;
					//查询
					$xianshi_st = _getOne($this->tb_xianshi_st,$str,$data) ;
					if (!$xianshi_st) {
						$this->ajaxReturn(array('message'=>'未参加活动！'));
					}
					unset($data);
					$data ['xianshi_id'] = I('post.xianshi_id') ;
					$data ['goods_id'] = I('post.goods_id') ;
					$data ['xianshi_explain'] = $xianshi_one ['xianshi_explain'] ;
					$data ['xianshi_title'] = $xianshi_one ['xianshi_title'] ;
					$data ['xianshi_name'] = $xianshi_one ['xianshi_name'] ;
					$data ['store_id'] = $xianshi_st ['st_id']  ;
					$data ['goods_name'] = $goods_one ['productname'] ;
					$data ['goods_price'] = $goods_one ['price'] ;
					$data ['xianshi_price'] = I('post.xianshi_price') ;
					$data ['goods_image'] = $goods_one ['pic'] ;
					$data ['start_time'] = time() ;
					$data ['end_time'] = $xianshi_one ['end_time'] ;
					$data ['lower_limit'] = $xianshi_one ['lower_limit'] ;
					$data ['state'] = 1 ;
					$data ['xianshi_recommend'] = 1 ;
					$data ['gc_id_1'] = $goods_one ['gc_id'] ;
					
				break;				  
			  }
				
				if ($table) {
					$add = _getAdd($table,$data);	
				}	
				$massege = '添加成功！' ;
				$error = '添加失败！' ;						
					
				if ($add) {
					unset($table) ;
					unset($data) ;
					$this->ajaxReturn(array('result'=>$massege));				
				} else {
					$this->ajaxReturn(array('message'=>$error));	
				}
		  }
		  
	  }
	  
	  
	/*
	*实物交易订单
	*
	*/
    public function st_order_list(){
		
		if ($_GET['p']) {
			$page = $_GET['p'] ;
		} else {
			$page = 1 ;
		}
		$num = 4 ;
		
		//所有的订单
		unset($str);
		unset($arr);
		unset($field);	
		$str = 'store_id=%d' ;
		$arr = array($_SESSION['shop_store']['store_id']) ;

		$field='*' ;
		$order='order_id desc' ; 
		//分页
		$show = Page($this->tb_order,$str,$arr,$num) ;
		
		$list = D($this->tb_order)->relation(true)->where($str,$arr)->field($field)->order($order)->page($page.','.$num)->select();
		
		foreach ($list as $key=>$v) {
			unset($pay_code);
			//支付方式
			$pay_code = _getOne($this->tb_pay, 'payment_code="%s"', array($v['payment_code'])) ;
			$list[$key] ['payment_name'] = $pay_code['payment_name'] ;
			$list[$key]['i'] = count($v['goods_list']);
		}		
		//dump($list);die;
		$this->assign('page',$show) ;
		$this->assign('list',$list) ;
		$this->display();
    }
	
	/**
	*未付款订单
	*/
	public function st_order_unpay() {
		if ($_GET['p']) {
			$page = $_GET['p'] ;
		} else {
			$page = 1 ;
		}
		$num = 4 ;
		//未付款订单
		unset($str);
		unset($arr);
		unset($field);	
		$str = 'store_id=%d and order_state=%d' ;
		$arr = array($_SESSION['shop_store']['store_id'],10) ;

		$field='*' ;
		$order='order_id desc' ; 
		//分页
		$show = Page($this->tb_order,$str,$arr,$num) ;
		
		$list = D($this->tb_order)->relation(true)->where($str,$arr)->field($field)->order($order)->page($page.','.$num)->select();
		
		foreach ($list as $key=>$v) {
			unset($pay_code);
			//支付方式
			$pay_code = _getOne($this->tb_pay, 'payment_code="%s"', array($v['payment_code'])) ;
			$list[$key] ['payment_name'] = $pay_code['payment_name'] ;
			$list[$key]['i'] = count($v['goods_list']);
		}		
		//dump($list);die;
		$this->assign('page',$show) ;
		$this->assign('list',$list) ;
		
		$this->display();
	}
	
	/**
	*待发货订单
	*/
	public function st_order_unsend() {
		if ($_GET['p']) {
			$page = $_GET['p'] ;
		} else {
			$page = 1 ;
		}
		$num = 4 ;
		//未付款订单
		unset($str);
		unset($arr);
		unset($field);	
		$str = 'store_id=%d and order_state=%d' ;
		$arr = array($_SESSION['shop_store']['store_id'],20) ;

		$field='*' ;
		$order='order_id desc' ; 
		//分页
		$show = Page($this->tb_order,$str,$arr,$num) ;
		
		$list = D($this->tb_order)->relation(true)->where($str,$arr)->field($field)->order($order)->page($page.','.$num)->select();
		
		foreach ($list as $key=>$v) {
			unset($pay_code);
			//支付方式
			$pay_code = _getOne($this->tb_pay, 'payment_code="%s"', array($v['payment_code'])) ;
			$list[$key] ['payment_name'] = $pay_code['payment_name'] ;
			$list[$key]['i'] = count($v['goods_list']);
		}		
		//dump($list);die;
		$this->assign('page',$show) ;
		$this->assign('list',$list) ;
		
		$this->display();
	}
	
	
	/**
	*已发货订单 待收货
	*/
	public function st_order_untake() {
		if ($_GET['p']) {
			$page = $_GET['p'] ;
		} else {
			$page = 1 ;
		}
		$num = 4 ;
		//未付款订单
		unset($str);
		unset($arr);
		unset($field);	
		$str = 'store_id=%d and order_state=%d' ;
		$arr = array($_SESSION['shop_store']['store_id'],30) ;

		$field='*' ;
		$order='order_id desc' ; 
		//分页
		$show = Page($this->tb_order,$str,$arr,$num) ;
		
		$list = D($this->tb_order)->relation(true)->where($str,$arr)->field($field)->order($order)->page($page.','.$num)->select();
		
		foreach ($list as $key=>$v) {
			unset($pay_code);
			//支付方式
			$pay_code = _getOne($this->tb_pay, 'payment_code="%s"', array($v['payment_code'])) ;
			$list[$key] ['payment_name'] = $pay_code['payment_name'] ;
			$list[$key]['i'] = count($v['goods_list']);
		}		
		//dump($list);die;
		$this->assign('page',$show) ;
		$this->assign('list',$list) ;
		
		$this->display();
	}
	
	
	/**
	*已完成订单
	*/
	public function st_order_finish() {
		if ($_GET['p']) {
			$page = $_GET['p'] ;
		} else {
			$page = 1 ;
		}
		$num = 4 ;
		//未付款订单
		unset($str);
		unset($arr);
		unset($field);	
		$str = 'store_id=%d and order_state=%d' ;
		$arr = array($_SESSION['shop_store']['store_id'],40) ;

		$field='*' ;
		$order='order_id desc' ; 
		//分页
		$show = Page($this->tb_order,$str,$arr,$num) ;
		
		$list = D($this->tb_order)->relation(true)->where($str,$arr)->field($field)->order($order)->page($page.','.$num)->select();
		
		foreach ($list as $key=>$v) {
			unset($pay_code);
			//支付方式
			$pay_code = _getOne($this->tb_pay, 'payment_code="%s"', array($v['payment_code'])) ;
			$list[$key] ['payment_name'] = $pay_code['payment_name'] ;
			$list[$key]['i'] = count($v['goods_list']);
		}		
		//dump($list);die;
		$this->assign('page',$show) ;
		$this->assign('list',$list) ;
		
		$this->display();
	}
	
	 /*
	 *
	 *获取订单信息
	 */ 
	public function change_state() {
		//提交hash防止数据重复提交
		$hash = shopHash('one_order_hash',I('post.order_sphash'));
		
		if (I('post.form_submit')=="ok" && $hash) {
			$state_type	= $_GET['state_type'];
			$order_id	= intval($_GET['order_id']);
			unset($str);
			unset($arr);
			unset($field);
			unset($join);
			$table = '__ORDER__ AS b' ;	
			$str = "b.order_id=%d and b.store_id=%d and b.order_state=%d" ;
			$arr = array($order_id, $_SESSION['shop_store']['store_id'], 10) ;
			$join = "LEFT JOIN __ORDER_GOODS__ as bg ON bg.order_id=b.order_id" ;
			$field = "*" ;
			
			$order_info = M()->field($field)->table($table)->join($join)->where($str, $arr)->find();
			
			if ($_GET['state_type'] == 'order_cancel') {
			   //$result = $this->_order_cancel($order_info,$_POST);
			} elseif ($_GET['state_type'] == 'modify_price') {
				//$result = $this->_order_ship_price($order_info,$_POST);
			} elseif ($_GET['state_type'] == 'spay_price') {
				//修改总价格
				$result = $this->_order_spay_price($order_info,I('post.'));
			}
			if (!$result) {
				die('<script>alert("订单修改失败！");history.back(-1);</script>');
			} else {
				die('<script>alert("修改成功，请刷新页面！");history.back(-1);</script>');
			}			
		} else {
			$this->redirect('/store/order_list');
		}

	}	  
	
	 /*
	 *
	 *修改订单价格
	 */ 
	public function _order_spay_price($order_info,$data) {

		unset($str);
		unset($arr);
		unset($updata);
		$str = "order_id=%d and store_id=%d and order_state=%d" ;
		$arr = array($order_info['order_id'], $_SESSION['shop_store']['store_id'], 10) ;
		$updata['order_amount'] = $data['goods_amount'] ;
		
		$result = _getEdit($this->tb_order, $str, $arr, $updata) ;
			
        return $result ;
	}
	
	/**
	*订单详情
	*/
	public function st_order_info() {
		if (I('get.order_id') && I('get.order_sn')) {

			$order_id	= intval(I('get.order_id'));
			unset($str);
			unset($arr);
			$str = 'store_id=%d and order_id=%d' ;
			$arr = array($_SESSION['shop_store']['store_id'], $order_id) ;
			$field='*' ;
			
			$order = D($this->tb_order)->relation(true)->where($str,$arr)->field($field)->find();
			
			//支付方式
			$pay_code = _getOne($this->tb_pay, 'payment_code="%s"', array($order['payment_code'])) ;
			$order ['payment_name'] = $pay_code['payment_name'] ;
			$order ['count'] = count($order ['goods_list']);	
			//dump($order);die;
			$this->assign('order',$order) ;
			$this->display();
		}
		
	}
	
	/**
	*发货设置
	*/
	public function st_order_send() {
		if (I('get.order_id') && I('get.act')=="store_deliver" && I('get.op')=="send") {

			$order_id	= intval(I('get.order_id'));
			unset($str);
			unset($arr);
			$str = 'store_id=%d and order_id=%d' ;
			$arr = array($_SESSION['shop_store']['store_id'], $order_id) ;
			$field='*' ;
			
			$order = D($this->tb_order)->relation(true)->where($str,$arr)->field($field)->find();
			
			//支付方式
			$pay_code = _getOne($this->tb_pay, 'payment_code="%s"', array($order['payment_code'])) ;
			$order ['payment_name'] = $pay_code['payment_name'] ;
			$order ['count'] = count($order ['goods_list']);	
			//dump($order);die;
			$this->assign('order',$order) ;
			$this->display();
		}	
	}
	
	/**
	*商家发货
	*/
	public function st_goods_send() {
		
		if (I('post.order_id') && I('post.form_submit')=="ok") {
			unset($data) ;
			unset($str) ;
			unset($arr) ;
			$str = "order_id=%d and store_id=%d and order_state=%d" ;
			$arr = array(I('post.order_id'), $_SESSION['shop_store']['store_id'] , 20) ;
			I('post.deliver_explain') && $data ['deliver_explain'] = htmlentities(I('post.deliver_explain')) ;//商家备忘
			//物流单号
			if (I('post.shipping_code')) {
				if (preg_match('/^\d+$/',I('post.shipping_code'))) {
					$data ['shipping_code'] = intval(I('post.shipping_code')) ;
				} else {
					die('<script>alert("物流单号必须是数字！");history.back(-1);</script>');
				}
			}
			$data ['order_state'] = 30 ;
			//dump($arr );die;
			$edit = _getEdit($this->tb_order, $str, $arr, $data) ;
			if ($edit) {
				$this->error('发货成功！','order_list');
			} else {
				die('<script>alert("发货失败！");history.back(-1);</script>');
			}
		}	
	}
	
	/*
	* 获取分区
	*/
	public function getNavArea() {
		$result = M('store') -> where('store_id = %d', session('shop_store.store_id'))->field('shop_area_ids')->select();
		$shop_area_ids = unserialize($result[0]['shop_area_ids']);
		unset($result);
		$result = M('shop_area') -> where('shop_area_state = 1 and shop_area_id in (%s)', implode(',', $shop_area_ids)) -> order('shop_area_sort ASC') -> select();
		$this->assign('area_list', $result);
		return true;
	}	
	
	/*
	* Ajax获取分区
	*/
	public function postNavArea() {
		
		if (IS_OST && I('post.atc')==="area") {
			
			$result = M('shop_area') -> where('shop_area_id = %d',I('post.shop_area_ids'))->field('shop_area_name,pay_id')->find();
			if ($result) {
				$row = M('payment') -> where('payment_id=%d and payment_state=%d',array($result['pay_id'],1))->field('payment_code,payment_id,payment_name,payment_state')->find();
				if (!$row) {
					$this->ajaxReturn(array('error'=>'该分区支付方式未开启'));
				}
				$data = array() ;
				$data['shop_area_name'] = $result['shop_area_name'] ;
				$data['payment_code'] = $row['payment_code'] ;
				$data['payment_name'] = $row['payment_name'] ;
				
				$this->ajaxReturn(array('info'=>$data));
			} else {
				$this->ajaxReturn(array('error'=>'该分区不存'));		
			}		
		}		

	}
	
}